package cn.tedu.store.user.webapi.controller;

import cn.tedu.store.commons.pojo.user.dto.AddressAddNewDTO;
import cn.tedu.store.commons.pojo.user.dto.AddressUpdateDTO;
import cn.tedu.store.commons.pojo.user.vo.AddressListItemVO;
import cn.tedu.store.commons.pojo.user.vo.AddressStandardVO;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.user.service.IAddressService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;
    
    @ApiOperation("添加收货地址")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('user')")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated AddressAddNewDTO addressAddNewDTO) {
        log.debug("开始处理添加收货地址的请求: 参数:{}", addressAddNewDTO);
        addressService.addNew(addressAddNewDTO);
        log.debug("添加收货地址成功!");
        return JsonResult.ok();
    }

    @ApiOperation("删除收货地址")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('user')")
    @PostMapping("/delete")
    public JsonResult<Void> delete(Long[] ids) {
        log.debug("开始处理[删除收货地址]的请求 参数:{}", ids);
        addressService.delete(ids);
        log.debug("删除收货地址成功!");
        return JsonResult.ok();
    }

    @ApiOperation("修改收货地址")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('user')")
    @PostMapping("/update")
    public JsonResult<Void> update(AddressUpdateDTO addressUpdateDTO) {
        log.debug("处理[修改收货地址]请求 参数:{}",addressUpdateDTO);
        addressService.updateById(addressUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("收货地址详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "地址id", required = true)
    @PreAuthorize("hasAuthority('user')")
    @GetMapping("/{id:[0-9]+}/get-standard")
    public JsonResult<AddressStandardVO> getById(@PathVariable Long id) {
        log.debug("开始处理[获取收货地址详情]请求 参数:{}",id);
        return JsonResult.ok(addressService.getStandardById(id));
    }

    @ApiOperation("收货地址列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int")
    })
    @PreAuthorize("hasAuthority('user')")
    @GetMapping("")
    public JsonResult<JsonPage<AddressListItemVO>> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理[获取收货地址列表]请求 参数:{},{}",pageNum, pageSize);
        return JsonResult.ok(addressService.list(pageNum, pageSize));
    }
}
